<?php
class site_index {
    var $db = null;
    var $smarty = null;
    var $msg = null;
	
	function site_index() {
        $this->smarty = new MySmarty();
        $this->db = new DbAccess();
    }
	
	function displayForm($formvars = array())
	{
		$this->smarty->assign('formvars',$formvars);
		
		if(isset($formvars['page']) && is_numeric($formvars['page'])){
			$page = $formvars['page'];
		}else{
			$page = 1;
		}
		
		$limit = "limit ".($page-1)*PAGE_SIZE.",".PAGE_SIZE;
		if(isset($formvars['title']) && $formvars['title']!=''){
		    $title = stripslashes($formvars['title']);
			$tag_query = "&title=".urlencode($formvars['title']);
		}else
		{
			$title="";
			$tag_query ="";
		}
		
		
		/**
		 * 総件数
		 */
		if($title!=''){
			$titleParam='%'.$title.'%';
			$sql = "SELECT COUNT(urllist_id) FROM urllist
			        WHERE blog_title like :title";
			$stmt = $this->db->prepare($sql);
			$stmt->bindParam(':title', $titleParam);
			
		}else{
			$sql = "SELECT COUNT(urllist_id) FROM urllist";
			$stmt = $this->db->prepare($sql);	
		}
		
		$stmt->execute();
		$count =$stmt->fetchColumn();
		$this->smarty->assign('count', $count);
		
		$stmt = null;
		
		/**
		 * 一覧
		 */
		if($title!=''){
			$titleParam='%'.$title.'%';
			$sql = "SELECT * FROM urllist u 
						left join country_mst c
							on  u.country_id=c.country_id
			     WHERE blog_title like :title
			     ORDER BY urllist_id desc
			     $limit";
			$stmt = $this->db->prepare($sql);
			$stmt->bindParam(':title', $titleParam);
			
		}else{
			$sql = "SELECT * FROM urllist u
						left join country_mst c
							on  u.country_id=c.country_id
			      ORDER BY urllist_id desc 
				  $limit";
			$stmt = $this->db->prepare($sql);
				
		}
		$stmt->execute();

		$bloglist = $stmt->fetchAll();
		
		$stmt = null;
		
		$this->smarty->assign('blogList', $bloglist);
		
		$this->smarty->assign('paging', $this->smarty->getNavigation("site_index.php",$page,$count,$tag_query));
		$this->smarty->assign('msg', $this->msg);
		$this->smarty->display( 'site_index.tpl' );
	}
	
	function deletBlog($formvars = array())
	{
		$BlogId=$formvars['blogId'];
		$count =$this->db->exec("DELETE FROM urllist WHERE urllist_id = '$BlogId'");
		if($count>0)
		{
			$this->msg = "ブログ[$BlogId]の削除に成功しました";
		}
		else
		{
			$this->msg = "ブログ[$BlogId]を削除できません";
		}
		$this->displayForm($formvars = array());
	}
}
?>